'use strict';

const BaseController = require('./base');
const { Op } = require('sequelize');
function parsePriceCondition(condition) {
    if (condition.startsWith('<')) {
        const price = parseFloat(condition.slice(1));
        return { [Op.lt]: price };
    } else if (condition.startsWith('>')) {
        const price = parseFloat(condition.slice(1));
        return { [Op.gt]: price };
    } else if (condition.includes('-')) {
        const [min, max] = condition.split('-').map(Number);
        return { [Op.between]: [min, max] };
    }
    return null;
}
class OfficeBuildingController extends BaseController {
    constructor(ctx) {
        super(ctx, 'officeBuilding');
    }

    getPageQuery(query) {
        let where = {};
        for (const queryKey in query) {
            if (queryKey === 'price' || queryKey === 'area') {
                const priceFilter = parsePriceCondition(query[queryKey]);
                if (priceFilter) {
                    where[queryKey] = priceFilter;
                }
            } else if (queryKey === 'building_name' ) {
                if (query[queryKey]) {
                    where = {
                        ...where,
                        [Op.or]: [
                            { building_name: { [Op.like]: `%${query[queryKey]}%` } },
                            { template_name: { [Op.like]: `%${query[queryKey]}%` } }
                        ]
                    }
                }
            } else {
                if (query[queryKey]) {
                    if (queryKey.includes('_id')) {
                        where[queryKey] = query[queryKey];
                    } else {
                        where[queryKey] = {
                            [Op.like]: `%${query[queryKey]}%`,
                        }
                    }
                }
            }

        }
        return where;
    }
}

module.exports = OfficeBuildingController;
